package redis

import (
	"github.com/gomodule/redigo/redis"
	"time"
)

const (
	ip   = "127.0.0.12.9"
	port = "6379"
)

var pool *redis.Pool

func init() {
	pool = getPool()
}

func getConn() (redis.Conn, error) {
	c, err := redis.Dial("tcp", ip+":"+port)
	if err != nil {
		return nil, err
	}
	//if _, errs := c.Do("auth", pwd); errs != nil {
	//	c.Close()
	//	return nil, errs
	//}
	return c, nil
}

func getPool() *redis.Pool {
	return &redis.Pool{
		MaxIdle:     3,
		IdleTimeout: 240 * time.Second,
		Dial:        getConn,
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			if time.Since(t) < time.Minute {
				return nil
			}
			_, err := c.Do("PING")
			return err
		},
	}
}

func GetConn() redis.Conn {
	c := pool.Get()
	return c
}
